home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1996, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- /* fog_func.c
- * This program demonstrates using the fog_func extension to
- * define a fog blending function.
- *
- * If the fog mode is GL_EXP or GL_EXP2, pressing the up and down
- * arrow keys adjusts the density value.
- *
- * If the fog mode is GL_LINEAR, pressing the up and down
- * arrow keys adjusts the end value for linear fog. The start value
- * for the linear fog is fixed.
- *
- * If the fog mode is GL_FOG_FUNC_SGI, an application defined
- * fog function is used.
- *
- * Escape key - exit the program
- * <f> key - change the fog blend function
- * UP Arrow Key - increase the fog density or end value
- * DOWN Arrow Key - decrease the fog density or end value
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
-
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid drawScene( GLvoid );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid animate( GLvoid );
- GLvoid visibility( GLint );
- GLvoid keyboard( GLubyte, GLint, GLint );
- GLvoid specialkeys( GLint, GLint, GLint );
-
- GLvoid cycleFog( GLvoid );
- GLvoid inc( GLvoid );
- GLvoid dec( GLvoid );
-
- void printHelp( char * );
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- /* Global Variables */
-
- static GLint fogMode;
- static GLfloat fogDensity;
- static GLfloat fogStart = 5.0;
- static GLfloat fogEnd = 11.0;
-
- static GLboolean fogFuncSupported = GL_TRUE;
-
- static GLfloat angle = 80.0;
-
- void
- main( int argc, char *argv[] )
- {
- GLsizei width, height;
-
- glutInit( &argc, argv );
-
- width = glutGet( GLUT_SCREEN_WIDTH );
- height = glutGet( GLUT_SCREEN_HEIGHT );
- glutInitWindowPosition( width/4, height/4 );
- glutInitWindowSize( width/2, height/2 );
- glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutKeyboardFunc( keyboard );
- glutSpecialFunc( specialkeys );
- glutReshapeFunc( reshape );
- glutIdleFunc( animate );
- glutVisibilityFunc( visibility );
- glutDisplayFunc( drawScene );
-
- printHelp( argv[0] );
- printf("\nFog mode is GL_EXP, density = %4.2f\n", fogDensity);
-
- glutMainLoop();
- }
-
- GLvoid
- printHelp( char *progname )
- {
- fprintf(stdout,"\n%s - demonstrates fog\n\n"\
- "Escape key - exit the program\n"
- "<f> key - change fog blend function\n"
- "UP Arrow Key - increase fog density or end value\n"
- "DOWN Arrow Key - decrease fog density or end value\n",
- progname
- );
- }
-
- /* Initialize lighting and materials, enable depth buffer, and set
- * initial fog parameters.
- */
- GLvoid
- initgfx( GLvoid )
- {
- GLfloat fogColor[4] = { 0.5, 0.5, 0.5, 1.0 };
- GLfloat fogSpline[64];
-
- /* mat_specular and mat_shininess are NOT default values */
- GLfloat mat_specular[] = { 0.8, 0.8, 0.8, 1.0 };
- GLfloat mat_shininess[] = { 10.0 };
-
- /* light_position is NOT default value */
- GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
-
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
-
- glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
- glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
-
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
-
- glEnable(GL_COLOR_MATERIAL);
- glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
-
- glEnable( GL_DEPTH_TEST );
-
- /* set the fog function and density */
- fogMode = GL_EXP;
- fogDensity = 0.25;
-
- /* setup and enable fog */
- glFogi( GL_FOG_MODE, fogMode );
- glFogfv( GL_FOG_COLOR, fogColor );
- glFogf( GL_FOG_DENSITY, fogDensity );
- glFogf( GL_FOG_START, fogStart );
- glFogf( GL_FOG_END, fogEnd );
- glHint( GL_FOG_HINT, GL_DONT_CARE );
- glEnable( GL_FOG );
-
- if (!glutExtensionSupported("GL_SGIS_fog_func")) {
- fogFuncSupported = GL_FALSE;
- fprintf( stderr,
- "GL_SGIS_fog_func not supported on this machine\n");
- }
-
- if (fogFuncSupported) {
- /* define our own fog function */
- fogSpline[0] = 0.2; fogSpline[1] = 1.0;
- fogSpline[2] = 0.8; fogSpline[3] = 0.8;
- fogSpline[4] = 1.0; fogSpline[5] = 0.6;
- fogSpline[6] = 3.0; fogSpline[7] = 0.4;
- fogSpline[8] = 10.0; fogSpline[9] = 0.35;
- fogSpline[10] = 20.0; fogSpline[11] = 0.3;
- fogSpline[12] = 50.0; fogSpline[13] = 0.27;
- fogSpline[14] = 70.0; fogSpline[15] = 0.2;
- fogSpline[16] = 90.0; fogSpline[17] = 0.05;
- fogSpline[18] = 100.0; fogSpline[19] = 0.0;
- #ifdef GL_SGIS_fog_func
- glFogFuncSGIS(10, fogSpline);
- #endif
- }
-
- /* clear background to the same color as the fog */
- glClearColor( fogColor[0], fogColor[1], fogColor[2], fogColor[3] );
- }
-
- GLvoid
- cycleFog( GLvoid )
- {
- if (fogMode == GL_EXP) {
- fogMode = GL_EXP2;
- printf( "Fog mode is GL_EXP2\n" );
- } else if (fogMode == GL_EXP2) {
- fogMode = GL_LINEAR;
- printf( "Fog mode is GL_LINEAR\n" );
- } else if (fogMode == GL_LINEAR) {
- #ifdef GL_SGIS_fog_func
- if (fogFuncSupported) {
- fogMode = GL_FOG_FUNC_SGIS;
- printf( "Fog mode is GL_FOG_FUNC_SGIS\n" );
- } else {
- fogMode = GL_EXP;
- printf( "Fog mode is GL_EXP\n" );
- }
- } else if (fogMode == GL_FOG_FUNC_SGIS) {
- #endif
- fogMode = GL_EXP;
- printf( "Fog mode is GL_EXP\n" );
- }
- glFogi( GL_FOG_MODE, fogMode );
- }
-
- GLvoid
- dec( GLvoid )
- {
- if (fogMode == GL_LINEAR) {
- fogEnd -= 0.5;
- if (fogEnd < fogStart) fogEnd = fogStart;
- glFogf( GL_FOG_END, fogEnd );
- printf( "Fog end = %4.2f\n", fogEnd );
- } else {
- fogDensity -= 0.05;
- if (fogDensity < 0.0) fogDensity = 0.0;
- glFogf( GL_FOG_DENSITY, fogDensity );
- printf( "Fog density = %4.2f\n", fogDensity );
- }
- }
-
- GLvoid
- inc( GLvoid )
- {
- if (fogMode == GL_LINEAR) {
- fogEnd += 0.5;
- if (fogEnd > 12.0) fogEnd = 12.0;
- glFogf( GL_FOG_END, fogEnd );
- printf( "Fog end = %4.2f\n", fogEnd );
- } else {
- fogDensity += 0.05;
- if (fogDensity > 1.0) fogDensity = 1.0;
- glFogf (GL_FOG_DENSITY, fogDensity);
- printf ("Fog density = %4.2f\n", fogDensity);
- }
- }
-
- GLvoid
- specialkeys( GLint key, GLint x, GLint y )
- {
- switch (key) {
- case GLUT_KEY_UP: /* increase fog density or end */
- inc();
- break;
- case GLUT_KEY_DOWN: /* decrease fog density or end */
- dec();
- break;
- }
- glutPostRedisplay();
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case 'f': /* cycle fog mode */
- cycleFog();
- glutPostRedisplay();
- break;
- case KEY_ESC: /* Exit whenever the Escape key is pressed */
- exit(0);
- }
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- GLdouble aspect;
-
- glViewport( 0, 0, width, height );
-
- aspect = (GLdouble) width / (GLdouble) height;
-
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluPerspective( 45.0, aspect, 3.0, 13.0 );
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- glTranslatef( 0.0, 0.0, -8.0 );
- }
-
- GLvoid
- animate( GLvoid )
- {
- /* update the current angle */
- angle = fmodf( (angle + 0.5), 360.0 );
-
- /* Tell GLUT to redraw the scene */
- glutPostRedisplay();
- }
-
- GLvoid
- visibility( int state )
- {
- if (state == GLUT_VISIBLE) {
- glutIdleFunc( animate );
- } else {
- glutIdleFunc( NULL );
- }
- }
-
- GLvoid
- drawScene( GLvoid )
- {
- int i, slices = 8;
-
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
-
- glPushMatrix();
-
- glRotatef( angle, 0.0, 1.0, 0.0 );
-
- for ( i = 0; i < slices; i++ )
- {
- glColor3f( i/10.0, i/10.0, 1.0 - i/10.0 );
- glPushMatrix();
- glRotatef( i * 360.0/slices, 0, 0, 1 );
- glTranslatef( 1.5, 0.0, 0.0 );
- glRotatef( i * 360.0/slices, 0, 1, 0 );
- glutSolidTorus( 0.25, 0.75, 15, 31 );
- glPopMatrix();
- }
-
- glPopMatrix();
-
- glutSwapBuffers();
-
- checkError("drawScene");
- }
-